package com.qn.controller;

import com.qn.entity.EmailContent;
import com.qn.service.EmailContentService;
import com.qn.config.AipContentCensorSelfConfig;
import com.qn.utils.Msg;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * (Emailcontent)表控制层
 *
 * @author ph
 * @since 2021-05-11 14:21:59
 */
@RestController
@RequestMapping("emailcontent")
public class EmailcontentController {
    /**
     * 服务对象
     */
    @Resource
    private EmailContentService emailcontentService;

    /**
     * 插入模板
     */
    @PostMapping("insertEmailTemplate")
    public Msg insertOneEmailTemplate(@RequestBody EmailContent emailcontent) {
        this.emailcontentService.insert(emailcontent);
        return Msg.success().add("success", 200);
    }

    /**
     * 检测邮件内容信息
     */
    @GetMapping("checkEmailContent")
    public Map<String, Object> checkEmailContent(String content) {
        return AipContentCensorSelfConfig.checkContext(content);
    }

    /**
     * 获得模板
     */
    @GetMapping("getEmailTemplate")
    public Msg getEmailTemplate(Integer id) {
        List<EmailContent> emailContentList = this.emailcontentService.getEmailTemplate(id);
        if (emailContentList.size() == 0) {
            return Msg.fail();
        }
        return Msg.success().add("templateOptions", emailContentList);
    }

    /**
     * 删除模板
     */
    @DeleteMapping("delete")
    public Msg deleteThisTemplate(Integer id) {
        if (!this.emailcontentService.deleteById(id)) {
            return Msg.fail();
        }
        return Msg.success();
    }
}